package hnlg.com.cn.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import hnlg.com.cn.mapper.UserMapper;
import hnlg.com.cn.vo.User;

@Service
public class MyUserDetailService implements UserDetailsService {

	@Autowired
	UserMapper userMapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userMapper.findByUsername(username);
		if(null == user){
			throw new UsernameNotFoundException("用户不存在");
		}
		List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles());
		user.setAuthorities(authorities);
		user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
		return user;
	}

}
